/**
 * 
 */
package test;


/**
 * @author Administrator
 * 
 */
public class PrintThread extends Thread {
	static long tt1 = System.currentTimeMillis();
	public static void main(String[] argv) {
		
		System.out.println("start");
		PrintThread t1 = new PrintThread("t1");
		PrintThread t2 = new PrintThread("t2");
		PrintThread t3 = new PrintThread("t3");
		t1.setNext(t2);
		t2.setNext(t3);
		t3.setNext(t1);

		t1.start();


		
	}

	public PrintThread(String data) {
		this.data = data;
	}

	public void setNext(PrintThread next) {
		this.next = next;
//		this.next.p = this;

	}

	public void run() {
		while (true) {
			if(PrintThread.i++ ==10000000)
			{
				                 
			
				long tt2 = System.currentTimeMillis();
				System.out.println(tt2-tt1);
				return;
			}
				
				synchronized (this)
			{
				//System.out.println(""+this.data);
				try {
					
					synchronized (this.next)
					{
					//	System.out.println(""+this.next.data);
						if(this.next.isAlive())
						{
							this.next.notify();
						}
						else
						{
							
							this.next.start();
							
						}
					}
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		
		}
	}

	private PrintThread next;

	private String data;
	static int i = 0;


}
